package wsepForum.persistance;

import java.util.Date;
import java.util.Vector;

import wsepForum.common.elements.*;
import wsepForum.common.elements.AForumElement.ForumElementType;
import wsepForum.persistance.exceptions.*;

/**
 * this interface responsible for the connection of the persistance to the actual data base
 * the interface contains only read\write methods and one Init method the activates the DB
 * (in case of new upload of the system or old upload of existing forum). this interface is
 * between the IPersistanceHandler and the data base itself (can be XML, SQL Server and etc...)
 * @author Ofir Tam and Ori Ossmy
 *
 */
public interface IDBConnector {


	/**
	 * init the new DB if neccessary
	 * @return boolean - if init succeed or not
	 */
	public boolean initDB() throws DataBaseIOException;

	/**
	 * clears the DB (for testing only)
	 * @throws Exception
	 */
	public void clearDB() throws DataBaseIOException;


	/**
	 * read a specific forum element according to a given ID - this method acitvated
	 * by the persistance handler - for returning the element
	 * @param elementID - the ID of the required element
	 * @return AForumElement - the required element asked by the persistance handler
	 * @preCondition there is element exist in the DB with ID like the given elementID
	 * @postCondition an element was built in the function according to the structure in the DB
	 * @postCondition no change in the DB
	 */
	public AForumElement read(int elementID, ForumElementType type) throws WrongElementIDException, DataBaseIOException;



	/**
	 * read from DB - and returns the User object according to some UserName given by
	 * the persistance handler
	 * @param userName - String represent the required user Name
	 * @return User - the required User
	 * @preCondition there is user exist in the DB with userName like the given userName
	 * @postCondition user was built in the function according to the structure in the DB
	 * @postCondition no change in the DB
	 */
	public User read(String userName) throws WrongUserNameException, DataBaseIOException;



	/**
	 * writes the given A forum element under the given parent ID in the DB -
	 * returns boolean if the action succeed or not
	 * @param parentID - the id of the element that contains the given forum element
	 * @param element - the given element to add or to modify
	 * @return boolean - if the action succeed or not
	 * @preCondition the given element has elementID different than -1
	 * @preCondition the given element has parentID different than -1
	 * @preCondition the given element has legal and complete structure
	 * @postCondition in case the elementID was not exist - a new node has added to DB with the given
	 * element structure.
	 * @postCondition in case the elementID was exist - the previous node was replaced by the new one
	 */
	public boolean write(AForumElement element) throws WrongElementIDException, DataBaseIOException;

	/**
	 * writes the given user in the DB or modify it (it's password)
	 * @param user - the user to modify
	 * @return boolean - if the action succeed or not
	 * @preCondition the given user has legal userName (unique) and encrypted password
	 * @postCondition - a new node represents the User structure added to the DB
	 */
	public boolean write(User user) throws DataBaseIOException;


	/**
	 * deletes the user from the DB according to some userName -
	 * used in Ipersistenence Handler to delete user authentications
	 * @param userName - the user name of the user we want to delete from DB
	 * @return boolean - if the action succeed or not
	 * @throws Exception
	 * @preCondition the user with the given userName exists in the DB
	 * @postCondition the user with the given userName was deleted from the system - does not exist
	 */
	public boolean delete(String userName) throws DataBaseIOException;



	/**
	 * deletes the element from the DB according to it's ID.
	 * @param elementID - the ID of the element we want to delete
	 * @param parentID - the ID of the parent of the element we want to delete
	 * @return boolean - if the action succeed or not
	 * @throws Exception
	 * @preCondition an element with the given elementID  exists in the DB
	 * @preCondition an element with the parentID exists in the DB
	 * @postCondition the element with the elementID and parentID like the given parentID was delete from DB
	 */
	public boolean delete(int elementID, ForumElementType type) throws DataBaseIOException;

	/**
	 * close open DB connections
	 */
	public void close();

	public Vector<String> getUsernameList() throws DataBaseIOException;

	public double[] getUserCountStatistics(Date date) throws DataBaseIOException;

	public double[] getMessageCountStatistics(Date date) throws DataBaseIOException;

	public double[] getUserMessagePerDay(Date fromDate, String userName) throws DataBaseIOException;

	public void setLoginTime(String strUserName) throws DataBaseIOException;

	public void setLogoutTime(String username) throws DataBaseIOException;

	//public boolean getLock(String lock) throws Exception;
	//public void releaseLock(String lock) throws Exception;

}
